package dk.degofedal.heating

import scala.actors.scheduler.ResizableThreadPoolScheduler
import scala.actors.Actor._

object HeatingControl {
  /*lazy val scheduler = {
   val s = new ResizableThreadPoolScheduler(false)
   s.start()
   s
   } */

  def main(args: Array[String]): Unit = {

    val caller = this
    var running: Boolean = true;

    /*val googleOutput = actor {
      //val helper = new GoogleHelper
      println("Google Output actor ready")
      while(running) {
        receive {
          case "quit" => println("Google Output actor quitting")
          case "helo" => println("Google Output actor saying hi")
          case  measurement: Measurement => println(measurement) //helper.updateSpreadsheet(measurement.time, measurement.val0, measurement.val1)
          case _ => println("ignoring")
        }
      }
    }*/

    /*val sendTweet = actor {
      // sends tweets from the system
      println("Send Tweet actor ready");
      while(running){
        receive {
          case "quit" => 
          case msg : String => "tweet"//TweetHelper.sendMessage("vaaben", msg);
        }
      }
     
      println("Send Tweet actor quitting");
    }*/

    val consoleOutput = actor {
      // handles output to the console
      println("Console Output actor ready");
      while (running) {
        receive {
          case "quit" => println("Console Output actor quitting");
          case msg => println(msg)
        }
      }
    }

    val avrInput = actor {
      // handle input from the avr

      val MatchMeasurement = """(\d+:\d+)\|(\d+)\|(\d+);""".r
      val MatchTime = """T(.+)""".r

      var alarmRaised: Boolean = false

      println("AVR Input actor ready");
      while (running) {
        receive {
          case "quit" => println("AVR Input actor quitting")
          case "helo" => println("AVR Input actor saying hi")
          case MatchTime(time) => println(time)
          /*case MatchMeasurement(time, t0, t1) => 
            val measurement = new Measurement(time,t0.toInt,t1.toInt)
            if(measurement.val0 < 50 && !alarmRaised) {
              // send alarm
              sendTweet ! "incomming temperature below warning threshold"
              alarmRaised = true
            }
            if(measurement.val0 > 55 && alarmRaised) {
              alarmRaised = false
            }
            consoleOutput ! measurement
            googleOutput ! measurement*/
          case msg => println(">" + msg)
        }
      }
    }

    val avr: AVR = new AVR(avrInput, "/dev/ttyACM0")
    avr.start

    val avrOutput = actor {
      // send messages to the avr
      println("AVR Output actor ready");
      while (running) {
        receive {
          case "quit" => println("AVR Output actor quitting")
          case msg: String => avr.send(msg)
        }
      }
      avr.stop
    }

    /*val recieveTweet = actor {
      // handles tweets send to the system
      println("Receive Tweet actor ready");
      while(running) {
       receive {
         case msg => println(msg)
       }
      }
      //TweetHelper.getMessages
      
      println("Receive Tweet actor quitting");
    }*/

    val consoleInput = actor {
      // handles input from the console
      println("Console Input actor ready");
      val tweetMatcher = """tweet(.+)\n+""".r
      while (running) {
        val input: String = Console.readLine
        input match {
          case "quit" =>
            running = false
            // send call of death to other actors
            avrInput ! "quit"
            avrOutput ! "quit"
            consoleOutput ! "quit"
            //googleOutput ! "quit"
            //recieveTweet ! "quit"
            //sendTweet ! "quit"
          case "setTime" =>
            avrOutput ! "CT" + System.currentTimeMillis + "R"
            //case tweetMatcher(msg) => sendTweet ! msg
          case _ =>
            avrOutput ! input
            consoleOutput ! input
        }
      }
      println("Console Input actor quitting");
    }
  }
}
